<?php
// $Id: index.php,v 1.21 2004/09/01 17:48:07 hthouzard Exp $
//  ------------------------------------------------------------------------ //
//                XOOPS - PHP Content Management System                      //
//                    Copyright (c) 2000 XOOPS.org                           //
//                       <http://www.xoops.org/>                             //
// ------------------------------------------------------------------------- //
//  This program is free software; you can redistribute it and/or modify     //
//  it under the terms of the GNU General Public License as published by     //
//  the Free Software Foundation; either version 2 of the License, or        //
//  (at your option) any later version.                                      //
//                                                                           //
//  You may not change or alter any portion of this comment or credits       //
//  of supporting developers from this source code or any supporting         //
//  source code which is considered copyrighted (c) material of the          //
//  original comment or credit authors.                                      //
//                                                                           //
//  This program is distributed in the hope that it will be useful,          //
//  but WITHOUT ANY WARRANTY; without even the implied warranty of           //
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the            //
//  GNU General Public License for more details.                             //
//                                                                           //
//  You should have received a copy of the GNU General Public License        //
//  along with this program; if not, write to the Free Software              //
//  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA //
//  ------------------------------------------------------------------------ //
include_once '../../mainfile.php';
include( XOOPS_ROOT_PATH . "/header.php" ) ;
$xoopsOption['template_main'] = "weather_index.html" ;

if(isset($_GET['degree'])){
	$xoopsModuleConfig['measurment'] = $_GET['degree'];
}

if(!isset($_GET['city'])){
	$result = $xoopsDB->query( "SELECT code FROM ".$xoopsDB->prefix("weather")." WHERE `default` = 'true' LIMIT 1" ) ;
	$myrow = $xoopsDB->fetchArray($result);
}else{
	$result = "spam";
	$myrow['code'] = $_GET['city'];
}


if($result){
   //try the different format at yahoo
	$url = "http://weather.yahoo.com/forecast/".$myrow['code']."_".$xoopsModuleConfig['measurment'].".html"; 
	$url2 = "http://xml.weather.yahoo.com/forecast/".$myrow['code']."_".$xoopsModuleConfig['measurment'].".html"; 
	if(!$lines = file ($url)){
		$lines = file ($url2);
	}
	


//#######################################
//breadcrumbs 
//#######################################
	$titleStart="";
	$titleEnd="";
	for($i=0; $i < count($lines); $i++){
		if(ereg('<h1>', $lines[$i])){
			$titleStart = $i;
			break;
		}
	}

		

	if($xoopsModuleConfig['measurment'] == 'f'){
		$xoopsTpl->assign('breadcrumb', "<div style='margin:3px 0 7px 0; font:normal arial;font-size:14px; font-weight:600;'>" .trim(strip_tags($lines[$titleStart]))." &nbsp; &nbsp; &nbsp; F | <a href='index.php?degree=c&amp;city=".$myrow['code']."'>C</a></div>");
	}else{
		$xoopsTpl->assign('breadcrumb', "<div style='margin:3px 0 7px 0; font:normal arial;font-size:14px; font-weight:600;'>" .trim(strip_tags($lines[$titleStart]))." &nbsp; &nbsp; &nbsp; <a href='index.php?degree=f&amp;city=".$myrow['code']."'>F</a> | C</div>");
	}
	





//#######################################
//forecast-module
//#######################################			
	$forecastMod_Start="";
	$forecastMod_Temp="";
	$detailed="";
	$records="";
	$forecastMod_End="";

	for($i=$titleStart; $i < count($lines); $i++){
		if(ereg('<div id="bd"', $lines[$i])){
			$forecastMod_Start = $i;
		}elseif(ereg('<div id="yw-breadcrumb">', $lines[$i])){
			$lines[$i] = "";
			$lines[$i+1] = "";
			$lines[$i+2] = "";
			$lines[$i+3] = "";
			$lines[$i+4] = "";
			$lines[$i+5] = "";
			$lines[$i+6] = "";
			$lines[$i+7] = "";
		}elseif(ereg('Detailed Forecast', $lines[$i])){
			$detailed = $i;
		}elseif(ereg('Records & Averages', $lines[$i])){
			$records = $i;
		}elseif(ereg('<div id="yw-fivedayforecast"', $lines[$i])){
			$forecastMod_End = $i-1;
			break;
		}
	}
	
	
	
		/*if($xoopsModuleConfig['show_detailed'] == 0){
			$lines[$detailed] = "<li>&raquo; <a href='http://xml.weather.yahoo.com/forecast/".$myrow['code']."_".$xoopsModuleConfig['measurment'].".html#text' class='action'>Detailed Forecast</a></li>";
		}else{
			$lines[$detailed] = "<li>&raquo; <a href='#local' class='action'>Detailed Forecast</a></li>";
		}
		$lines[$records] = "<li>&raquo; <a href='http://xml.weather.yahoo.com/climo/".$myrow['code']."_".$xoopsModuleConfig['measurment'].".html#text' class='action'>Records & Averages</a></li>";
		*/

		
	$forecastMod="";
	if($forecastMod_Start!=""){
		for($q=$forecastMod_Start; $q <= $forecastMod_End; $q++){
				$forecastMod = $forecastMod . $lines[$q];
		}
	}
			$xoopsTpl->assign('forecast_mod', $forecastMod);


//#######################################
//5-Day Outlook
//#######################################	
			if($xoopsModuleConfig['show_5day'] == 1){
				for($i=($forecastMod_End-2); $i < count($lines); $i++){
					if(ereg('<div id="yw-fivedayforecast"', $lines[$i])){
						$fiveDay_Start = $i; 
					}elseif(ereg('<th class="nobg"', $lines[$i])){
						$lastColumn = $i;
					}elseif(ereg('<td rowspan="2" class="extended">', $lines[$i])){
						$lastColumn2 = $i;
					}elseif(ereg('<div id="yw-alerts">', $lines[$i])){
						$fiveDay_End = $i-1;
						break;
					}
					
				}
				$lines[$lastColumn] = substr($lines[$lastColumn],0,strrpos ($lines[$lastColumn], '<th class="nobg"'));
				
				$lines[$lastColumn2]= substr($lines[$lastColumn2],0,strrpos ($lines[$lastColumn2], '<td rowspan="2" class="extended">'));
								
					$fiveDayDisplay="";
						if($fiveDay_Start!=""){
							for($q=$fiveDay_Start; $q <= $fiveDay_End; $q++){
								$fiveDayDisplay = $fiveDayDisplay . $lines[$q];
							}
						}
			

				$xoopsTpl->assign('fiveDay', $fiveDayDisplay);
			}
			
			
		
//#######################################
//Detailed Forecast
//#######################################				
			if($xoopsModuleConfig['show_detailed'] == 1){

				for($i=$forecastMod_End; $i < count($lines); $i++){
					if(ereg('<div id="yw-detailedforecast">', $lines[$i])){
						$detailed_Start = $i;
					}elseif(ereg('<h3>', $lines[$i])){
						$lines[$i]="";
						$lines[$i+1]="";
					}elseif(ereg('<div> &raquo;', $lines[$i])){
						$detailed_End = $i-1;
						break;
					}
					
				}
				



				$detailDisplay="<a name='local'><h3 style='padding-bottom:0px; margin;0'>Detailed Local Forecast</h3></a>";
						if($detailed_Start!=""){
							for($q=$detailed_Start; $q <= $detailed_End; $q++){
								$detailDisplay = $detailDisplay . $lines[$q];
							}
						}





				$xoopsTpl->assign('detailed', $detailDisplay);
			}
	
}else{
echo"No weather results available at this time.";
}


//Select form to change city view
	$result = $xoopsDB->query( "SELECT * FROM ".$xoopsDB->prefix("weather")." ORDER By name ASC") ;
		$data = array();
		if ( $result ) {
			while ( $myrow = $xoopsDB->fetchArray($result) ) {
				$data[]=$myrow;
			}
		}
		$select="<form style='margin:0; padding:0;' action='index.php' method='post' name='citySelect'><select name=\"city\" size=\"1\" onChange='changeCity()'><option value='' selected>Change City</option>";
		foreach($data as $cities){
			$select = $select . "<option value='".$cities['code']."'>".$cities['name']."</option>";
		}
		$select = $select."</select></form>";
		$xoopsTpl->assign('select', $select);
		
		
include_once XOOPS_ROOT_PATH.'/footer.php';
?>
